############################################
###### Replication Code for Figure A3 ######
########### January 9th, 2025 ##############
############################################

### Script produces Figure A3 and saves it in the `/figures` folder

rm(list=ls())

# load libraries
library(dplyr)
library(fixest)
library(ggplot2)
library(stringr)
library(MatchIt)
library(marginaleffects)
library("lmtest") #coeftest
library("sandwich") #vcovCL

# load data
df <- readRDS("data/workfile.rds")
w <- readRDS("data/weighted_party_feeling.rds")

# join weights to main data
df <- df %>% left_join(w)

# non-missing dataframe
nomiss <- df %>% 
  select(open_immig, dem_female, dem_country_code, dem_education_level,
         dem_country_code, dem_rural, dem_age, lrscale, parfam_name,
         high_income, medium_income, low_income, political_interest_num,
         high_edu, medium_edu, low_edu, treated, mean_outparty_feeling, 
         high_pol_interest, mean_close_num, d_affiliated, treated,
         political_interest, treated_econ, treated_culture, 
         trust_in_opposing_parties) %>% 
  mutate(coDE = ifelse(dem_country_code == "DE", 1, 0), 
         coFR = ifelse(dem_country_code == "FR", 1, 0), 
         coNL = ifelse(dem_country_code == "NL", 1, 0),
         coUS = ifelse(dem_country_code == "US", 1, 0),
         coPL = ifelse(dem_country_code == "PL", 1, 0),
         coGR = ifelse(dem_country_code == "GR", 1, 0), 
         coIT = ifelse(dem_country_code == "IT", 1, 0), 
         coSE = ifelse(dem_country_code == "SE", 1, 0), 
         coGB = ifelse(dem_country_code == "GB", 1, 0), 
         coES = ifelse(dem_country_code == "ES", 1, 0), 
         parfam_liberal = ifelse(parfam_name == "Liberal parties", 1, 0), 
         parfam_other = ifelse(parfam_name == "Other parties", 1, 0), 
         parfam_eco = ifelse(parfam_name == "Ecological parties", 1, 0), 
         parfam_nat = ifelse(parfam_name == "Nationalist parties", 1, 0), 
         parfam_left = ifelse(parfam_name == "Socialist/left parties", 1, 0), 
         parfam_cd = ifelse(parfam_name == "Christian democratic parties", 1, 0), 
         parfam_nonvoters = ifelse(parfam_name == "Non-voters", 1, 0), 
         parfam_sd = ifelse(parfam_name == "Social democratic parties", 1, 0), 
         parfam_con = ifelse(parfam_name == "Conservative parties", 1, 0), 
         parfam_ethnic = ifelse(parfam_name == "Ethnic and regional parties", 1, 0),
         parfam_spec = ifelse(parfam_name == "Special issue party", 1, 0),) %>% 
  .[complete.cases(.), ]

# generate empty data frame for coefs
coef_df <- data.frame()

##### Open immigration - Econ
nomiss1 <- nomiss %>% 
  filter((open_immig == 1 & treated_econ == 1) | treated == 0) %>% 
  .[complete.cases(.), ]
mod_match <- matchit(open_immig ~ as.factor(dem_country_code) +
                       as.factor(dem_education_level) + 
                       dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                       high_pol_interest + d_affiliated,
                     method = "optimal", data = nomiss1)

dta_m <- match.data(mod_match)
dim(dta_m)

dta_m$t <- scale(dta_m$trust_in_opposing_parties)

t_immig_econ <- feols(t ~ open_immig + as.factor(dem_country_code) +
                        as.factor(dem_education_level) + 
                        dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                        high_pol_interest + d_affiliated, dta_m, weights = dta_m$weights, vcov = "hetero") 



temp <- data.frame(coef = c(t_immig_econ$coefficients[2]), 
                   se = c(t_immig_econ$se[2]),
                   name = "Immigration",
                   var = c("Trust"), 
                   group = "Economic issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp)


##### Open immigration - Culture

nomiss1 <- nomiss %>% 
  filter((open_immig == 1 & treated_culture == 1) | treated == 0) %>% 
  .[complete.cases(.), ]
mod_match <- matchit(open_immig ~ as.factor(dem_country_code) +
                       as.factor(dem_education_level) + 
                       dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                       high_pol_interest + d_affiliated,
                     method = "optimal", data = nomiss1)
dta_m <- match.data(mod_match)
dim(dta_m)

dta_m$t <- scale(dta_m$trust_in_opposing_parties)


t_immig_cult <- feols(t ~ open_immig + as.factor(dem_country_code) +
                        as.factor(dem_education_level) + 
                        dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                        high_pol_interest + d_affiliated, dta_m, weights = dta_m$weights,
                      vcov = "hetero") %>% summary()

temp <- data.frame(coef = c(t_immig_cult$coefficients[2]),
                   se = c(t_immig_cult$se[2]),
                   name = "Immigration",
                   var = c("Trust"), 
                   group = "Cultural issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp)




######## No immigration - Cultural condition

nomiss1 <- nomiss %>% 
  filter((open_immig == 0 & treated_culture == 1) | treated == 0) %>%
  .[complete.cases(.), ]
mod_match <- matchit(treated ~ as.factor(dem_country_code) +
                       as.factor(dem_education_level) + 
                       dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                       high_pol_interest + d_affiliated,
                     method = "optimal", data = nomiss1)
dta_m <- match.data(mod_match)
dim(dta_m)

dta_m$t <- scale(dta_m$trust_in_opposing_parties)

t_no_immig_total <- feols(t ~ treated + as.factor(dem_country_code) +
                            as.factor(dem_education_level) + 
                            dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                            high_pol_interest + d_affiliated, dta_m, weights = dta_m$weights,
                          vcov = "hetero") %>% summary()

temp <- data.frame(coef = c(t_no_immig_total$coefficients[2]),
                   se = c(t_no_immig_total$se[2]),
                   name = "No immigration",
                   var = c("Trust"), 
                   group = "Cultural issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp)



######## No immigration - Econ
nomiss1 <- nomiss %>% 
  filter((open_immig == 0 & treated_econ == 1) | treated == 0) %>% 
  .[complete.cases(.), ]
mod_match <- matchit(treated ~ as.factor(dem_country_code) +
                       as.factor(dem_education_level) + 
                       dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                       high_pol_interest + d_affiliated,
                     method = "optimal", data = nomiss1)
dta_m <- match.data(mod_match)
dim(dta_m)

dta_m$t <- scale(dta_m$trust_in_opposing_parties)

t_no_immig_econ <- feols(t ~ treated+ as.factor(dem_country_code) +
                           as.factor(dem_education_level) + 
                           dem_age + parfam_nat + parfam_left + parfam_con + mean_close_num + mean_outparty_feeling +
                           high_pol_interest + d_affiliated, dta_m, weights = dta_m$weights,
                         vcov = "hetero") %>% summary()

temp <- data.frame(coef = c(t_no_immig_econ$coefficients[2]), 
                   se = c(t_no_immig_econ$se[2]),
                   name = "No immigration",
                   var = c("Trust"), 
                   group = "Economic issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp)


coef_df <- rbind.data.frame(coef_df, temp)
coef_df <- coef_df %>% 
  filter(group != "All")


# Generate figure

pd <- position_dodge(0.5)

regs <- ggplot(coef_df, aes(x = group, ymin = coef - se*1.96, y = coef, ymax = coef + se*1.96, color = name)) + 
  geom_point(position = pd, shape = 21, fill = 'white') + 
  geom_errorbar(aes( ymin = coef - se*1.959964, ymax = coef + se*1.959964), width = 0, position = pd) + 
  geom_errorbar(aes( ymin = coef - se*1.644854, ymax = coef + se*1.644854), width = 0, position = pd, linewidth = 1.2) + 
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  scale_colour_manual(values = c("black", "grey")) + 
  xlab("") + 
  ylab("Effect of discussing immigration on out-partisan trust") +
  coord_flip() +
  facet_wrap(~ var) + 
  theme_bw() + 
  guides(color=guide_legend(title=""))

regs

ggsave("figures/FigA3_matching_predictors.pdf",
        height = 4, width = 6)


